package com.light.lallalalalal;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.ui.activity.BaseGameActivity;

public class Picture implements IUpdateHandler{

	public BaseGameActivity game;
	public Sprite sprite;
	public Texture texture;
	public TextureRegion textureRegion;
	
	boolean rightOrLeft = false;
	boolean touched = false;
	
	public Picture(BaseGameActivity game)
	{	
		this.game = game;
	}
	
	public void onLoadEngine()
	{
		
	}
	
	public void onLoadResource()
	{
		texture = new Texture(32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		textureRegion = TextureRegionFactory.createFromAsset(texture, game, "face_box.png", 0, 0);

		game.getEngine().getTextureManager().loadTexture(this.texture);
	}
	
	public void onLoadScene(Scene scene, MyPhysicsWorld physicsWorld)
	{
		sprite = new Sprite(100, 100, textureRegion) {
			@Override
        	public boolean onAreaTouched(TouchEvent event, float pTouchAreaLocalX, float pTouchAreaLocalY)
        	{
				switch (event.getAction())
				{
				case TouchEvent.ACTION_DOWN:
					touched = true;
					break;
				case TouchEvent.ACTION_MOVE:
					if (touched)
					{
						sprite.setPosition(event.getX() - sprite.getWidth() / 2, event.getY() - sprite.getHeight() / 2);
					}
					break;
				case TouchEvent.ACTION_UP:
					touched = false;
					break;
				}
				return true;
        	}
		};
		sprite.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		sprite.setSize(50, 50);
		sprite.setRotation(100);
		sprite.setAlpha(0.2f);
		
		scene.attachChild(sprite);
		physicsWorld.registeUpdateHandler(this);
		scene.registerTouchArea(sprite);
	}

	@Override
	public void onUpdate(float pSecondsElapsed) {
		if (rightOrLeft)
		{
			sprite.setPosition(sprite.getX() + 1, sprite.getY());
			if (sprite.getX() >= 400)
				rightOrLeft = !rightOrLeft;
		}
		else
		{
			sprite.setPosition(sprite.getX() - 1, sprite.getY());
			if (sprite.getX() <= 100)
				rightOrLeft = !rightOrLeft;
		}
	}
	@Override
	public void reset() {
		// TODO Auto-generated method stub
		
	}
	
}
